package com.example.iotest.bio;

import com.sun.org.apache.xerces.internal.impl.io.UTF8Reader;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.junit.Test;
import sun.nio.cs.UnicodeEncoder;
import sun.text.normalizer.UTF16;

import java.io.*;
import java.nio.charset.Charset;
import java.util.*;

public class Test4 {

    public static void ff() {
        System.out.println();
    }

    public static void main(String[] args) {
        String fileName = "D:\\Resources\\GitProjects\\io-test\\io-test\\io-test\\src\\main\\resources\\files\\1.txt";
        File file = new File(fileName);
        try (FileInputStream fileInputStream = new FileInputStream(file)) {
            System.out.println(fileInputStream.available());
        } catch (IOException e) {
            e.printStackTrace();
        }
        ff();
        String s = "10011";
        System.out.println("s: " + return10(s));
        System.out.println(0xffffffff);
        byte b = -1;
        System.out.println((char) b);
        System.out.println((int) (char) b);
        System.out.println((int) (char) (b & 0xff));
        System.out.println("(int)'a' " + (int) (char) -2);
        StringBuilder stringBuilder = new StringBuilder("s");
    }

    public static int return10(String binary) {
        int num = 0;
        for (int i = binary.length() - 1; i >= 0; i--) {
            if (binary.charAt(i) == '1')
                num += (int) Math.pow(2, binary.length() - i - 1);
        }
        return num;
    }

    @Test
    public void f1() throws IOException {
        File file1 = new File("target/classes/files/2.txt");
        System.out.println(file1.exists() + " " + file1.length());
        BufferedReader fileReader = new BufferedReader(new FileReader(file1));
        String s = fileReader.readLine();
        String s2 = fileReader.readLine();
        System.out.println(s2);
        byte b1 = 127;
        System.out.println(new Object().hashCode());
        System.out.println(new Test4().hashCode());
        System.out.println(Integer.toBinaryString(-3));
        System.out.println(Integer.toBinaryString(5));
        System.out.println((char) 313333 + " " + (int) 'അ' + " " + (int) '発' + " " + (int) '死' + " " + (int) '了');
        new Character('⛄');
        int i1 = UTF16.charAt("2", 0);
        System.out.println("i1 " + i1);
        System.out.println(UTF16.valueOf(66666));
        /*for (int i = 0; i < 65536; i++) {
            System.out.print((char)i+" ");
            if (i%50==0)
                System.out.println();
        }*/
        FileWriter fileWriter = new FileWriter("src/main/resources/files/1.txt");
        for (int i = 0; i < 100000; i++) {
            System.out.print(UTF16.valueOf(i) + " ");
            fileWriter.write(UTF16.valueOf(i) + " ");
            if (i % 50 == 0) {
                System.out.println();
                fileWriter.write("\n");
            }
        }
        fileWriter.close();
    }

    @Test
    public void f2() {
        HashMap<String, String> map = new HashMap<>();
        map.put("1", "01");
        map.put(null, "0_null");
        map.put("2", "02");
        map.put("3", "03");
        String s = map.get(null);
        for (Map.Entry<String, String> entry : map.entrySet()
        ) {
            System.out.println(entry.getKey() + " " + entry.getValue());
        }
//        System.out.println(s);
//        System.out.println(-1 >>> 1);
//        System.out.println(-1 >> 1);
//        Hashtable<String, String> hashtable = new Hashtable<>();
//        hashtable.put(null, "1");
//        hashtable.put(null, null);
        System.out.println("---------");
        /*for (int i = 0; i < 10; ++i) {
            System.out.println(i++);
        }
        System.out.println("---------");
        for (int i = 0; i < 10; i++) {
            System.out.println(++i);
        }*/
        int i = 0;
        System.out.println(i++);
        System.out.println(i);
        System.out.println(++i);
        System.out.println(Float.MAX_VALUE);
        System.out.println(Float.MIN_VALUE);
        float f = -1.0f;
        String s1 = Integer.toBinaryString(Integer.MIN_VALUE);
        System.out.println(s1);
        int iii = 0b01000001000010000000000000000000;
        System.out.println(10.626f);
        char a = (char) -1;
        System.out.println(0.11 + 2001299.32);
        String s2 = Float.toHexString(0.09999999999999998f);
        String ss2 = Float.toHexString(1 + 0.99f);
        System.out.println(s2 + "\n" + ss2);
        System.out.println("(int)iii " + iii);
        System.out.println("(int)iii " + (float) iii);

        System.out.println(1.e-12 == Math.pow(10, -12));
        System.out.println(1e-12 == Math.pow(10, -12));
        System.out.println(1e-5 == Math.pow(10, -5));
        System.out.println((byte) (0b11111111 + 0b00000001));

        Float f1 = 1.7f;
        Float f2 = 0.1f;
        float f3 = f1 + f2;
        System.out.println("f1 + f2 = " + f3);

        System.out.println(2.0f - 1.9f);
    }


    //优化冒泡排序,flag记录是否交换,position记录交换位置
    @Test
    public void f4() {
        int[] ints1 = new int[]{7, 5, 0, 8, 1, 2, 3, 9, 4, 6};
        int[] ints2 = new int[]{9, 8, 7, 0, 6, 5, 4, 3, 2, 1};
        int[] ints3 = new int[]{8, 6, 9, 0, 7, 5, 4, 3, 2, 1};
        int[] ints = ints3;
        int len = ints.length;
        int temp;
        int flag;
        int position = 0;
        for (int i = 0; i < len - 1; i++) {
            flag = 0;
            for (int j = 0; j < len - 1 - i; j++) {
                if (ints[j] < ints[j + 1]) {
                    temp = ints[j];
                    ints[j] = ints[j + 1];
                    ints[j + 1] = temp;
                    flag = 1;
                    position = j;
                }
            }
            len = position + 1;
            if (flag == 0) {
                System.out.println("第" + (i + 1) + "轮: " + Arrays.toString(ints));
                break;
            }
            System.out.println("第" + (i + 1) + "轮: " + Arrays.toString(ints));
        }
        System.out.println(Arrays.toString(ints));
    }


    @Test
    public void ff1() {
        int[] ints = new int[]{9, 2, 8, 9, 1, 4, 0, 3, 5, 7, 6, 9, 8};
        Node[] arr = new Node[5];
        int n;
        for (int anInt : ints) {
            n = anInt;
            Node newNode = new Node(n);
            Node node;
            switch (n) {
                case 0:
                case 1:
                    node = arr[0];
                    arr[0] = Node.sortedList(node, newNode);
                    break;
                case 2:
                case 3:
                    node = arr[1];
                    arr[1] = Node.sortedList(node, newNode);
                    break;

                case 4:
                case 5:
                    node = arr[2];
                    arr[2] = Node.sortedList(node, newNode);
                    break;

                case 6:
                case 7:
                    node = arr[3];
                    arr[3] = Node.sortedList(node, newNode);
                    break;

                case 8:
                case 9:
                    node = arr[4];
                    arr[4] = Node.sortedList(node, newNode);
                    break;
                default:
                    break;
            }
        }

        for (Node node : arr) {
            Node.bianLiList(node);
        }
    }

}

@AllArgsConstructor
@NoArgsConstructor
class Node {
    int i;
    Node next;
    Node pre;

    Node(int i) {
        this.i = i;
    }

    public static Node sortedList(Node node, Node newNode) {
        Node current = node, pre, next;
        int newI = newNode.i;
        if (current != null) {
            while (newI > current.i && current.next != null) {
                current = current.next;
            }
            if (current.pre == null) {
                newNode.next = current;
                current.pre = newNode;
                return newNode;
            } else if (current.next != null) {
                pre = current.pre;
                next = current.next;
                pre.next = newNode;
                newNode.pre = pre;
                newNode.next = next;
                next.pre = newNode;
                return node;
            } else {
                current.next = newNode;
                newNode.pre = current;
                return node;
            }
        } else {
            return newNode;
        }

    }

    public static void bianLiList(Node node) {
        while (node != null) {
            System.out.println(node.i);
            node = node.next;
        }
    }
}